<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个性化仪表盘演示</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background-color: #f5f5f5;
            color: #333;
        }

        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 20px;
            text-align: center;
        }

        .container {
            max-width: 1200px;
            margin: 20px auto;
            padding: 0 20px;
        }

        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }

        .stat-card {
            background: white;
            border-radius: 10px;
            padding: 25px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            text-align: center;
            transition: transform 0.3s ease;
        }

        .stat-card:hover {
            transform: translateY(-5px);
        }

        .stat-icon {
            width: 60px;
            height: 60px;
            border-radius: 50%;
            margin: 0 auto 15px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            color: white;
        }

        .stat-icon.projects { background: #4CAF50; }
        .stat-icon.tasks { background: #2196F3; }
        .stat-icon.publications { background: #FF9800; }
        .stat-icon.completion { background: #E91E63; }

        .stat-number {
            font-size: 36px;
            font-weight: bold;
            color: #333;
            margin-bottom: 5px;
        }

        .stat-label {
            color: #666;
            font-size: 14px;
        }

        .charts-section {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
            margin-bottom: 30px;
        }

        .chart-card {
            background: white;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }

        .chart-title {
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 15px;
            color: #333;
        }

        .chart-container {
            height: 300px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: #f9f9f9;
            border-radius: 5px;
            color: #666;
        }

        .activities-section {
            background: white;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }

        .section-title {
            font-size: 20px;
            font-weight: bold;
            margin-bottom: 20px;
            color: #333;
        }

        .activity-item {
            padding: 15px;
            border-bottom: 1px solid #eee;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .activity-item:last-child {
            border-bottom: none;
        }

        .activity-description {
            flex: 1;
            color: #333;
        }

        .activity-time {
            color: #666;
            font-size: 12px;
        }

        .loading {
            text-align: center;
            color: #666;
            padding: 20px;
        }

        .error {
            color: #f44336;
            text-align: center;
            padding: 20px;
        }

        .login-section {
            background: white;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }

        .login-form {
            display: flex;
            gap: 10px;
            align-items: center;
            flex-wrap: wrap;
        }

        .login-form input {
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 14px;
        }

        .login-form button {
            padding: 10px 20px;
            background: #2196F3;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
        }

        .login-form button:hover {
            background: #1976D2;
        }

        .status {
            margin-top: 10px;
            padding: 10px;
            border-radius: 5px;
        }

        .status.success {
            background: #e8f5e8;
            color: #2e7d32;
        }

        .status.error {
            background: #ffebee;
            color: #c62828;
        }

        @media (max-width: 768px) {
            .charts-section {
                grid-template-columns: 1fr;
            }
            
            .stats-grid {
                grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            }
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>🎯 个性化仪表盘演示</h1>
        <p>基于用户身份的学术科研管理数据可视化</p>
    </div>

    <div class="container">
        <!-- 登录区域 -->
        <div class="login-section">
            <h3>登录测试</h3>
            <div class="login-form">
                <input type="text" id="username" placeholder="用户名" value="sbxnh">
                <input type="password" id="password" placeholder="密码" value="123456">
                <button onclick="login()">登录</button>
                <button onclick="loadDashboard()">刷新数据</button>
            </div>
            <div id="loginStatus"></div>
        </div>

        <!-- 统计卡片 -->
        <div class="stats-grid">
            <div class="stat-card">
                <div class="stat-icon projects">📊</div>
                <div class="stat-number project-count">-</div>
                <div class="stat-label">参与项目</div>
            </div>
            <div class="stat-card">
                <div class="stat-icon tasks">✅</div>
                <div class="stat-number task-count">-</div>
                <div class="stat-label">分配任务</div>
            </div>
            <div class="stat-card">
                <div class="stat-icon publications">📚</div>
                <div class="stat-number publication-count">-</div>
                <div class="stat-label">发表论文</div>
            </div>
            <div class="stat-card">
                <div class="stat-icon completion">🎯</div>
                <div class="stat-number completion-rate">-</div>
                <div class="stat-label">完成率</div>
            </div>
        </div>

        <!-- 图表区域 -->
        <div class="charts-section">
            <div class="chart-card">
                <div class="chart-title">项目状态分布</div>
                <div class="chart-container" id="project-status-chart">
                    图表加载中...
                </div>
            </div>
            <div class="chart-card">
                <div class="chart-title">任务优先级分布</div>
                <div class="chart-container" id="task-priority-chart">
                    图表加载中...
                </div>
            </div>
        </div>

        <!-- 最近活动 -->
        <div class="activities-section">
            <div class="section-title">📋 最近活动</div>
            <div class="recent-activities">
                <div class="loading">加载中...</div>
            </div>
        </div>
    </div>

    <script>
        const API_BASE = 'http://localhost:8080';
        let token = '';

        // 登录功能
        async function login() {
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const statusDiv = document.getElementById('loginStatus');

            try {
                const response = await fetch(`${API_BASE}/auth/login`, {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ username, password })
                });

                if (response.ok) {
                    const data = await response.json();
                    token = data.token;
                    localStorage.setItem('token', token);
                    statusDiv.innerHTML = '<div class="status success">✅ 登录成功！</div>';
                    
                    // 自动加载仪表盘数据
                    setTimeout(loadDashboard, 1000);
                } else {
                    statusDiv.innerHTML = '<div class="status error">❌ 登录失败</div>';
                }
            } catch (error) {
                statusDiv.innerHTML = '<div class="status error">❌ 网络错误</div>';
            }
        }

        // 加载仪表盘数据
        async function loadDashboard() {
            if (!token) {
                token = localStorage.getItem('token');
                if (!token) {
                    alert('请先登录');
                    return;
                }
            }

            try {
                const response = await fetch(`${API_BASE}/dashboard/data`, {
                    headers: {
                        'Authorization': `Bearer ${token}`,
                        'Content-Type': 'application/json'
                    }
                });

                if (response.ok) {
                    const data = await response.json();
                    updateDashboard(data);
                } else {
                    console.error('获取仪表盘数据失败:', response.status);
                }
            } catch (error) {
                console.error('网络错误:', error);
            }
        }

        // 更新仪表盘显示
        function updateDashboard(data) {
            console.log('仪表盘数据:', data);

            // 更新统计卡片
            if (data.userSummary) {
                document.querySelector('.project-count').textContent = data.userSummary.totalProjects || 0;
                document.querySelector('.task-count').textContent = data.userSummary.totalTasks || 0;
                document.querySelector('.publication-count').textContent = data.userSummary.totalPublications || 0;
            }

            if (data.taskStatistics) {
                const completionRate = data.taskStatistics.totalTasks > 0 
                    ? Math.round((data.taskStatistics.completedTasks / data.taskStatistics.totalTasks) * 100)
                    : 0;
                document.querySelector('.completion-rate').textContent = `${completionRate}%`;
            }

            // 更新项目状态图表
            if (data.chartData && data.chartData.projectStatusChart) {
                const projectChart = document.getElementById('project-status-chart');
                projectChart.innerHTML = data.chartData.projectStatusChart.map(item => 
                    `<div>${item.name}: ${item.value}</div>`
                ).join('<br>') || '暂无数据';
            }

            // 更新任务优先级图表
            if (data.chartData && data.chartData.taskPriorityChart) {
                const taskChart = document.getElementById('task-priority-chart');
                taskChart.innerHTML = data.chartData.taskPriorityChart.map(item => 
                    `<div>${item.name}: ${item.value}</div>`
                ).join('<br>') || '暂无数据';
            }

            // 更新最近活动
            if (data.recentActivities) {
                const activitiesContainer = document.querySelector('.recent-activities');
                if (data.recentActivities.length > 0) {
                    activitiesContainer.innerHTML = data.recentActivities.map(activity => `
                        <div class="activity-item">
                            <div class="activity-description">${activity.description}</div>
                            <div class="activity-time">${activity.timestamp}</div>
                        </div>
                    `).join('');
                } else {
                    activitiesContainer.innerHTML = '<div class="loading">暂无最近活动</div>';
                }
            }
        }

        // 页面加载时尝试自动登录
        window.onload = function() {
            const savedToken = localStorage.getItem('token');
            if (savedToken) {
                token = savedToken;
                loadDashboard();
            }
        };
    </script>
</body>
</html>
